package com.web_bloks.content.client;

import com.google.gwt.user.client.Timer;
import com.web_bloks.user.client.UserMessageServer;
/**
 * 
 * @author Jean-Lou Dupont
 *
 */
abstract public class UserMessageManager 
	implements UserMessageServer {

	Timer timer = null;
	
	final static int DEFAULT_TIMEOUT = 10000;

	/*****************************************
	 * CONSTRUCTOR
	 *****************************************/
	protected UserMessageManager() {
		super();
	}
	/*****************************************
	 * CONFIGURATION
	 *****************************************/
	
	/*****************************************
	 * API
	 *****************************************/
	
	public void sendMessage(String msg) {
		pushMessage(msg);
		updateTimer();
	}
	
	/*****************************************
	 * TEMPLATE PATTERN
	 *****************************************/
	/**
	 * Method called on sub-classes to 'push' 
	 * a message to the user.
	 */
	abstract protected void pushMessage(String msg);
	
	/**
	 * Method called on sub-classes to indicate 
	 * the passage of a 'tick' event.
	 */
	abstract protected void tick();
	
	/*===================================================================
	 * Timer 
	 ===================================================================*/
	protected void updateTimer() {

		if ( null != timer ) {
			timer.cancel();
			timer = null;
		}
		
		timer = new Timer() {
			public void run() {
				tick();
			}
		};
		
		timer.schedule(DEFAULT_TIMEOUT);
	}
	
}//END